﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCms.Core.Developers;
using MVCms.Core.Entities;
using MVCms.Core.Services;
using Plugin.Admin.Controlbox.Models;

namespace Plugin.Admin.Controlbox.Controllers
{
    public class ControlboxController : BaseController
    {
        [PluginModule(Name = "AdminControlBox", Description = "AdminControlBox", DisplayName = "AdminControlBox")]
        [HttpGet]
        public PartialViewResult ControlList()
        {
            AdminControlBoxModel model = new AdminControlBoxModel();
            model.EditMode = EditMode;
            IEnumerable<PluginModule> modules = PluginService.GetModules();
            model.Controls = modules;

            IEnumerable<VirtualPage> virtualPages = PageService.GetPages();
            model.Pages = virtualPages;

            return PartialView(model);
        }

        [HttpPost]
        public ActionResult ControlList(AdminControlBoxModel model)
        {
            EditMode = model.EditMode;
            new PageModule
                {
                    Title = model.Title,
                    Module = PluginService.GetModule(model.Control),
                    Panel = "1",
                    Position = 0,
                    VirtualPage = PageService.GetPage(model.Page)
                }.Save();

            return Redirect(Request.UrlReferrer.ToString());
        }
    }
}
